package com.glory.hikvisionmobile.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.DieBond160Adapter;
import com.glory.hikvisionmobile.adapter.DieBondSpecialAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.TrackOut.DieCarrierLoadBean;
import com.glory.hikvisionmobile.bean.TrackOut.XPdaDieSortingAndDieBound;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Work_DieBondActivity extends BaseActivity {

    @BindView(R.id.etCarrier)
    FreeUI_ClearEditText etCarrier;
    @BindView(R.id.rbSpecialDieBond)
    RadioButton rbSpecialDieBond;
    @BindView(R.id.rb160DieBond)
    RadioButton rb160DieBond;
    @BindView(R.id.etSpecial)
    FreeUI_ClearEditText etSpecial;
    @BindView(R.id.rvSpecialList)
    RecyclerView rvSpecialList;
    @BindView(R.id.llSpecialLayout)
    LinearLayout llSpecialLayout;
    @BindView(R.id.et160)
    FreeUI_ClearEditText et160;
    @BindView(R.id.rv160List)
    RecyclerView rv160List;
    @BindView(R.id.ll160Layout)
    LinearLayout ll160Layout;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    private String lotID;
    private List<XPdaDieSortingAndDieBound> xPdaDieSortingAndDieBounds;
    private DieBondSpecialAdapter dieBondSpecialAdapter;
    private DieBond160Adapter dieBond160Adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_die_bond;
    }

    @Override
    protected void initView() {
        lotID = getIntent().getExtras().getString("lotID");
        showLayout(true);
        rvSpecialList.setLayoutManager(new LinearLayoutManager(Work_DieBondActivity.this));
        rvSpecialList.setItemAnimator(new DefaultItemAnimator());
        rvSpecialList.addItemDecoration(new DividerItemDecoration(Work_DieBondActivity.this, 1));
        dieBondSpecialAdapter = new DieBondSpecialAdapter(null);
        dieBondSpecialAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        dieBondSpecialAdapter.isFirstOnly(true);
        rvSpecialList.setAdapter(dieBondSpecialAdapter);
        rv160List.setLayoutManager(new LinearLayoutManager(Work_DieBondActivity.this));
        rv160List.setItemAnimator(new DefaultItemAnimator());
        rv160List.addItemDecoration(new DividerItemDecoration(Work_DieBondActivity.this, 1));
        dieBond160Adapter = new DieBond160Adapter(null);
        dieBond160Adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        dieBond160Adapter.isFirstOnly(true);
        rv160List.setAdapter(dieBond160Adapter);
        etCarrier.requestFocus();
        FreeApi_EditTextUtils.setEditTextListener(etCarrier, this, s -> {
            int visibility = llSpecialLayout.getVisibility();
            if (visibility == View.GONE){
                et160.setText("");
                et160.requestFocus();
            }else {
                etSpecial.setText("");
                etSpecial.requestFocus();
            }
        });

        FreeApi_EditTextUtils.setEditTextListener(etSpecial, this, s -> {
            if (editIsEmpty(etCarrier,"设备载具号不能为空")) {
                searchDieBond(s,"SpecialDiebond");
            }
        });

        FreeApi_EditTextUtils.setEditTextListener(et160, this, s -> {
            if (editIsEmpty(etCarrier,"设备载具号不能为空")) {
                searchDieBond(s,"160Diebond");
            }
        });


    }

    private void searchDieBond(String carrier,String type) {
        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotID);
        map.addProperty("carrierId",etCarrier.getText().toString().trim());
        map.addProperty("dieBoundType",type);
        map.addProperty(type.equals("160Diebond")? "dbCarrierId" : "cleanReleaseCarrierId",carrier);
        NetUtils.requestDara("HIK.PdaDieCarrierLoad",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<DieCarrierLoadBean>>(
                new TypeToken<BaseResponseBean<DieCarrierLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<DieCarrierLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    xPdaDieSortingAndDieBounds = response.body().getBody().getxDieBounds();
                    if (xPdaDieSortingAndDieBounds.size() <= 0) {
                        showConfirmDialog("当前批次未绑定载具");
                        return;
                    }

                    dieBondSpecialAdapter.setNewData(xPdaDieSortingAndDieBounds);
                    dieBond160Adapter.setNewData(xPdaDieSortingAndDieBounds);



                }
            }

            @Override
            public void onError(Response<BaseResponseBean<DieCarrierLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIKPdaDieCarrierLoad");

            }
        });

    }


    @Override
    protected void initData() {
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("DieBond");
        return new FreeUI_AddViewEntity("header", llMain);
    }


    @OnClick({R.id.rbSpecialDieBond, R.id.rb160DieBond,R.id.tvReplay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbSpecialDieBond:
                clear();
                showLayout(true);
                break;
            case R.id.rb160DieBond:
                clear();
                showLayout(false);
                break;
            case R.id.tvReplay:
                if (editIsEmpty(etCarrier,"载具号不能为空")){
                    submit();
                }
                break;
        }
    }

    private void submit() {
        String dieBond = "";
        String dieType = "";
        String carrier = "";
        if (rb160DieBond.isChecked()) {
            dieBond = "160Diebond";
            dieType = "dbCarrierId";
            if (editIsEmpty(et160,"DB载具不能为空")){
                carrier = et160.getText().toString().trim();
            }

        }else {
            dieBond = "SpecialDiebond";
            dieType = "cleanReleaseCarrierId";
            if (editIsEmpty(etSpecial,"清洗载具不能为空")){
                carrier = etSpecial.getText().toString().trim();
            }
        }

        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotID);
        map.addProperty("carrierId",etCarrier.getText().toString().trim());
        map.addProperty("dieBoundType",dieBond);
        map.addProperty(dieType,carrier);
        NetUtils.requestDara("HIK.PdaDieCarrierSubmit",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<DieCarrierLoadBean>>(
                new TypeToken<BaseResponseBean<DieCarrierLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<DieCarrierLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())) {
                   showConfirmDialog(Work_DieBondActivity.this,"绑定成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<DieCarrierLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.PdaDieCarrierSubmit");

            }
        });
    }

    private void clear(){
        et160.setText("");
        etSpecial.setText("");
        etCarrier.setText("");
        dieBondSpecialAdapter.setNewData(null);
        dieBond160Adapter.setNewData(null);
    }

    private void showLayout(boolean isSpecialDieBond) {
        ll160Layout.setVisibility(isSpecialDieBond ? View.GONE : View.VISIBLE);
        llSpecialLayout.setVisibility(isSpecialDieBond ? View.VISIBLE : View.GONE);
    }
}
